package cn.itsource.org.mapper;

import cn.itsource.basic.domain.DeparmentQuery;
import cn.itsource.basic.mysuper.MyBaseMapper;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Department;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

public interface DepartmentMapper extends MyBaseMapper<Department> {
    default Class<Department> getTClass(){
        return Department.class;
    }


    default PageList<Department> page(DeparmentQuery query){
        //创建分页对象
        PageHelper.startPage(query.getPage(),query.getPageSize());
        //创建Example
        Example example = new Example(getTClass());
        //创建标准
        Example.Criteria criteria = example.createCriteria();
        Example.Criteria criteria1 = example.createCriteria();
        //根据del进行显示查询
        criteria.andEqualTo("del",1);
        //高级查询判断关键词是否为null
        if (Objects.nonNull(query.getKeyword())){
            criteria1.andLike("name","%"+query.getKeyword()+"%");
        }
        //拼接
        example.and(criteria1);
        //查询所有
        List<Department> departments = this.selectByExample(example);
        //创建分页
        PageInfo<Department> pageInfo = new PageInfo<>(departments);
        //返回分页对象
        return new PageList<>(pageInfo.getTotal(),pageInfo.getList());
    }

    @Select("select * from t_department where del=1")
    List<Department> finAll();

    @Select("select * from t_department where id=#{id} and del=1")
    Department findOne(Long id);
}
